﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Web.Settle
{
    public partial class Settle_Sell : System.Web.UI.Page
    {
        public double VarietyCount = 0;
        public double StorageMoney = 0;
        public double VarietyInterest = 0;
        public double VarietyMoney = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Request.QueryString["WBID"] != null)
                {
                    txtWBID.Value = SQLHelper.ExecuteScalar(" SELECT  TOP 1 strName FROM dbo.WB WHERE ID=" + Request.QueryString["WBID"].ToString()).ToString();
                }
                if (Request.QueryString["WBName"] != null)
                {
                    string WBName = Request.QueryString["WBName"].ToString();
                    string dtStart = Request.QueryString["dtStart"].ToString();
                    string dtEnd = Request.QueryString["dtEnd"].ToString();

                }
                else
                {
                    Qdtend.Value = DateTime.Now.ToString("yyyy-MM-dd");
                    Qdtstart.Value = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                }

            }
        }
        private void BindData(string WBName, string dtStart, string dtEnd)
        {


            //获取存粮信息
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSqlPara = new StringBuilder();//查询参数
            strSql.Append("  SELECT A.ID,B.strName AS WBName,C.AccountNumber,C.strName AS DepName,'存转销结算' AS BusinessName,CONVERT(NVARCHAR(100),A.dt_Sell,23) AS dt_Sell,DATEDIFF(DAY,A.dt_Sell,GETDATE()) AS dt_SellDay,");
            strSql.Append(" A.VarietyName,A.UnitName,A.VarietyCount,A.Price_JieSuan, A.StorageMoney,A.VarietyInterest,A.VarietyMoney,A.ISReturn");
            strSqlPara.Append("  FROM dbo.StorageSell A INNER JOIN dbo.WB B ON A.WBID=B.ID");
            strSqlPara.Append("  INNER JOIN dbo.Depositor C ON A.Dep_AccountNumber=C.AccountNumber");
            strSqlPara.Append(" where 1=1 ");
            if (WBName != "")
            {
                strSqlPara.Append("   AND B.strName = '" + WBName + "'");
            }
            if (dtStart != "")
            {
                strSqlPara.Append("   AND A.dt_Sell> '" + dtStart + "'");
            }
            if (dtEnd != "")
            {
                strSqlPara.Append("   AND A.dt_Sell < '" + dtEnd + "'");
            }
            strSql.Append(strSqlPara.ToString());

            DataTable dt = SQLHelper.ExecuteDataTable(strSql.ToString());
            Repeater1.DataSource = dt;
            Repeater1.DataBind();

            VarietyCount = 0; VarietyInterest = 0; StorageMoney = 0; VarietyMoney = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["ISReturn"].ToString() == "0")
                {
                    VarietyCount += Convert.ToDouble(dt.Rows[i]["VarietyCount"]);
                    VarietyInterest += Convert.ToDouble(dt.Rows[i]["VarietyInterest"]);
                    StorageMoney += Convert.ToDouble(dt.Rows[i]["StorageMoney"]);
                    VarietyMoney += Convert.ToDouble(dt.Rows[i]["VarietyMoney"]);
                }
                else {
                    VarietyCount -= Convert.ToDouble(dt.Rows[i]["VarietyCount"]);
                    VarietyInterest -= Convert.ToDouble(dt.Rows[i]["VarietyInterest"]);
                    StorageMoney -= Convert.ToDouble(dt.Rows[i]["StorageMoney"]);
                    VarietyMoney -= Convert.ToDouble(dt.Rows[i]["VarietyMoney"]);
                }
               
            }
            VarietyCount = Math.Round(VarietyCount, 2);
            VarietyInterest = Math.Round(VarietyInterest, 2);
            StorageMoney = Math.Round(StorageMoney, 2);
            VarietyMoney = Math.Round(VarietyMoney, 2);


        }

        public string GetVarietyMoney(object VarietyMoney,object ISReturn)
        {
            if (Convert.ToInt32(ISReturn) == 0)//第一次存转销的记录
            {
                return "<span style='color:Green'>" + VarietyMoney + "</span>";
            }
            else {
                return "<span style='color:Red'>-" + VarietyMoney + "</span>";
            }
        }

        public string GetOperateInfo(object ID)
        {
            string strReturn = "";
            //查询当前的这笔记录是否已经有结算的记录
            object objID = SQLHelper.ExecuteScalar("SELECT ID FROM dbo.SA_Sell WHERE StorageSellID=" + ID);
            if (objID == null || objID.ToString() == "")
            {
                strReturn = "  <a style='color:Red; font-weight:bold;' href='#' onclick='GetSettleSingle(" + ID + ")'>未结算</a>";

            }
            else
            {
                strReturn = "<span style='color:Green'>已结算</span>&nbsp;<a style='color:Blue; font-weight:bold;' href='#' onclick='PrintPage(" + ID + ")'>结算单据</a>";
            }
            return strReturn;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            object objWB = SQLHelper.ExecuteScalar(" SELECT  COUNT(ID) FROM dbo.WB WHERE strName ='" + txtWBID.Value.Trim() + "'");
            if (Convert.ToInt32(objWB) <= 0)
            {
                Fun.Alert("您查询的网点不存在");
                return;
            }
            if (Qdtstart.Value.Trim() == "")
            {
                Fun.Alert("请输入开始查询日期");
                return;
            }
            if (Qdtend.Value.Trim() == "")
            {
                Fun.Alert("请输入结束查询日期");
                return;
            }
            BindData(txtWBID.Value.Trim(), Qdtstart.Value.Trim(), Qdtend.Value.Trim());

        }
    }
}